#!/bin/bash

set -e
set -o xtrace

# CONTEXT: HOST prior to IMAGE BUILD as SCRIPT USER
# PURPOSE: Setup the requirements file for use by 15-reddwarf-dep

source $_LIB/die

TROVE_BRANCH=${TROVE_BRANCH:-'master'}
REQUIREMENTS_FILE=${TROVESTACK_SCRIPTS}/../../requirements.txt

[ -n "$TMP_HOOKS_PATH" ] || die "Temp hook path not set"
[ -e ${REQUIREMENTS_FILE} ] || die "Requirements not found"
[ -n "$HOST_USERNAME" ] || die "HOST_USERNAME not set"

sudo -Hiu ${HOST_USERNAME} dd if=${REQUIREMENTS_FILE} of=${TMP_HOOKS_PATH}/requirements.txt

UC_FILE=upper-constraints.txt
UC_DIR=$(pwd)
UC_BRANCH=${TROVE_BRANCH##stable/}

curl -L -o "${UC_DIR}/${UC_FILE}" "https://releases.openstack.org/constraints/upper/${UC_BRANCH}"
if [ -f "${UC_DIR}/${UC_FILE}" ]; then
    sudo -Hiu ${HOST_USERNAME} dd if="${UC_DIR}/${UC_FILE}" of=${TMP_HOOKS_PATH}/${UC_FILE}
    rm -f "${UC_DIR}/${UC_FILE}"
fi
